// pages/mys/follow/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tmpTotal: 0,
    tmpList: [
        // {uid: '1111', addtime: '2024-06-03'},
    ],
    pageTmpStart: 1,
    pageTmpLimit: 10,
    flagTmp: 0, //0还有，1没有了
    shopTotal: 0,
    shopList: [
        // {uid: '2222', addtime: '2024-06-03'},
    ],
    pageShopStart: 1,
    pageShopLimit: 10,
    flagShop: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTmpList();
    this.getShopList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 返回
  toBack: function (e) {
    console.log(e);
    wx.navigateBack({
      delta: 1
    }); return
  },

  // 获取个人数据
  getTmpList: function (e) {
    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    var start = this.data.pageStart;
    var limit = this.data.pageLimit;
    var flag = this.data.flagTmp;
    if (flag == 1) {
        wx.showToast({
          title: '没有了', icon: 'none'
        }); return
    }
    var formData = {
        wxid: userInfo.wxid,
        type: 1,
        start: start,
        limit: limit,
    };
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/my/follow/list',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        var dtotal = res.data.total;
        var dlist = res.data.lists;
        var total = that.data.shopTotal;
        var lists = that.data.shopList;
        var totalNew = total*1 + dtotal;
        var listNew = lists;
        for (var i = 0; i < dlist.length; i ++) {
            listNew.push(dlist[i]);
        }
        if (dlist.length == limit) {
            start ++;
        } else {
            flag = 1;
        }
        that.setData({
          tmpTotal: totalNew,
          tmpList: listNew,
          pageTmpStart: start,
          flagTmp: flag,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },

  // 获取商户数据
  getShopList: function (e) {
    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    var start = this.data.pageStart;
    var limit = this.data.pageLimit;
    var flag = this.data.flagShop;
    if (flag == 1) {
        wx.showToast({
          title: '没有了', icon: 'none'
        }); return
    }
    var formData = {
        wxid: userInfo.wxid,
        type: 1,
        start: start,
        limit: limit,
    };
    wx.request({
      method: 'POST',
      url: app.globalData.domainXcx + '/my/follow/list',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res);
        if (res.code!=0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        var dtotal = res.data.total;
        var dlist = res.data.lists;
        var total = that.data.shopTotal;
        var lists = that.data.shopList;
        var totalNew = total*1 + dtotal;
        var listNew = lists;
        for (var i = 0; i < dlist.length; i ++) {
            listNew.push(dlist[i]);
        }
        if (dlist.length == limit) {
            start ++;
        } else {
            flag = 1;
        }
        that.setData({
          shopTotal: totalNew,
          shopList: listNew,
          pageShopStart: start,
          flagShop: flag,
        });
      },
      fail: function () {
        console.log('error');
      }
    });
  },
})